package io.renren.modules.xm.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.DateUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.xm.entity.CustomerEntity;
import io.renren.modules.xm.service.CustomerService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 客户表
 *
 * @author liuyonggang
 * @email 544872316@qq.com
 * @date 2018-06-28 15:59:53
 */
@RestController
@RequestMapping("xm/customer")
public class CustomerController extends AbstractController {
    @Autowired
    private CustomerService customerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("xm:customer:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = customerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("xm:customer:info")
    public R info(@PathVariable("id") Integer id){
        CustomerEntity customer = customerService.selectById(id);

        return R.ok().put("customer", customer);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("xm:customer:save")
    public R save(@RequestBody CustomerEntity customer){
        customer.setCreateUserId(getUserId().intValue());
        customer.setModifyUserId(getUserId().intValue());
        customerService.add(customer);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("xm:customer:update")
    public R update(@RequestBody CustomerEntity customer){
        ValidatorUtils.validateEntity(customer);
        customer.setModifyTi(DateUtils.getCurrentTime());
        customer.setModifyUserId(getUserId().intValue());
        customerService.updateAllColumnById(customer);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("xm:customer:delete")
    public R delete(@RequestBody Integer[] ids){
        customerService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     *消费
     */
    @RequestMapping("/pay")
    @RequiresPermissions("xm:customer:pay")
    public R pay(@RequestBody CustomerEntity customer){
        ValidatorUtils.validateEntity(customer);
        customer.setModifyUserId(getUserId().intValue());
        customerService.pay(customer);
        return R.ok();
    }

    /**
     * 充值
     */
    @RequestMapping("/recharge")
    @RequiresPermissions("xm:customer:recharge")
    public R recharge(@RequestBody CustomerEntity customer){
        ValidatorUtils.validateEntity(customer);
        customer.setModifyUserId(getUserId().intValue());
        customerService.recharge(customer);
        return R.ok();
    }

    /**
     * 赠送
     */
    @RequestMapping("/zengsong")
    @RequiresPermissions("xm:customer:zengsong")
    public R zengsong(@RequestBody CustomerEntity customer){
        ValidatorUtils.validateEntity(customer);
        customer.setModifyUserId(getUserId().intValue());
        customer.setCreateUserId(getUserId().intValue());
        customerService.zengsong(customer);
        return R.ok();
    }

}
